/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/*
 * tienda.java
 *
 * Created on 24-feb-2011, 19:09:23
 */

/**
 *
 * @author Victor Manuel
 */
public class tienda extends javax.swing.JFrame {
   
private ResultSet sr;
 private Login l= new  Login();
    /** Creates new form tienda */
    public tienda() {
        initComponents();
        guardar.setVisible(false);//boton
        jLabel1.setVisible(false);
        jLabel2.setVisible(false);
        jLabel3.setVisible(false);
        jLabel4.setVisible(false);
        jLabel5.setVisible(false);
        nom.setVisible(false);
        marc.setVisible(false);
         clav.setVisible(false);
         prec.setVisible(false);
   cantidad.setVisible(false);
   selecciona.setVisible(false);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nom = new javax.swing.JTextField();
        marc = new javax.swing.JTextField();
        clav = new javax.swing.JTextField();
        prec = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        guardar = new javax.swing.JButton();
        cantidad = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        imagen = new javax.swing.JLabel();
        cambiar = new javax.swing.JButton();
        elim = new javax.swing.JButton();
        editar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        fecha = new javax.swing.JSpinner();
        selecciona = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        getContentPane().add(nom, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 70, 90, -1));
        getContentPane().add(marc, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 100, 90, -1));
        getContentPane().add(clav, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 130, 90, -1));
        getContentPane().add(prec, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 160, 90, -1));

        jLabel1.setText("Nombre:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 70, -1, -1));

        jLabel2.setText("Marca:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 100, -1, -1));

        jLabel3.setText("Clave:");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 130, -1, -1));

        jLabel4.setText("Precio:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 160, -1, -1));

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));
        jPanel1.setForeground(new java.awt.Color(153, 153, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        guardar.setText("guardar cambios");
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarActionPerformed(evt);
            }
        });
        jPanel1.add(guardar, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 220, -1, 30));

        cantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cantidadActionPerformed(evt);
            }
        });
        jPanel1.add(cantidad, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 190, 90, -1));

        jLabel5.setText("cantidad");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 190, -1, -1));

        imagen.setIcon(new javax.swing.ImageIcon("C:\\Users\\Victor Manuel\\Pictures\\THCAIO6AMGCA2966O5CAEMWYR0CADZI08QCA67UVYLCAWVPJUECAXPTK5CCAF28P0CCA0VX3Y4CAC5STWNCALXHHOWCAOESURMCAU9MSJ2CAEEVJD8CAYFA433CAUFIXHCCA3YT1L2CAMMYI7S.jpg")); // NOI18N
        jPanel1.add(imagen, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 30, 230, 240));

        cambiar.setText("Cambiar contraseña");
        cambiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cambiarActionPerformed(evt);
            }
        });
        jPanel1.add(cambiar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 270, -1, -1));

        elim.setText("Eliminar");
        elim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                elimActionPerformed(evt);
            }
        });
        jPanel1.add(elim, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 10, 80, -1));

        editar.setText("Editar");
        editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editarActionPerformed(evt);
            }
        });
        jPanel1.add(editar, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 10, 70, -1));

        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 10, 80, -1));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Marca", "Clave", "Precio", "cantidad"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 290, 210));

        jLabel6.setFont(new java.awt.Font("Tahoma", 3, 18));
        jLabel6.setText("Tabla de productos");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, -1, -1));

        fecha.setModel(new javax.swing.SpinnerDateModel());
        jPanel1.add(fecha, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 20, 110, -1));

        selecciona.setFont(new java.awt.Font("Tahoma", 3, 12));
        selecciona.setText("Selecciona el productoa editar");
        jPanel1.add(selecciona, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 40, -1, -1));

        jButton4.setText("reporte total de compras");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 270, 190, -1));

        jButton2.setText("inicio");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 270, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 570, 310));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarActionPerformed

         try {

            @SuppressWarnings("static-access")
          java.sql.Statement st = l.conexion.createStatement();
        st.executeUpdate("update productos set nombres='"+nom.getText()+"', marca='"+marc.getText()+"',clave='"+clav.getText()+"',precio='"+prec.getText()+"',cantidad='"+cantidad.getText()+"' where clave='"+eliminar+"'");
        new tienda().setVisible(true);
          this.dispose();
          guardar.setVisible(false);
        } catch (SQLException ex) {
            Logger.getLogger(contrasen.class.getName()).log(Level.SEVERE, null, ex);
        }
//


        // TODO add your handling code here:
    }//GEN-LAST:event_guardarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
new Agregar().setVisible(true);
        this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void elimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_elimActionPerformed

        try {
        java.sql.Statement  st = l.conexion.createStatement();
          System.out.println(eliminar+" ggg");
//         JOptionPane.showConfirmDialog(rootPane, "¿Está seguro de que decea eliminar el producto?");
        int x=st.executeUpdate("delete from productos where clave='"+eliminar+"'");
        new tienda().setVisible(true);
        this.dispose();
        datosTabla();
        } catch (SQLException ex) {
            Logger.getLogger(tienda.class.getName()).log(Level.SEVERE, null, ex);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_elimActionPerformed

    private void editarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarActionPerformed
guardar.setVisible(true);
jLabel1.setVisible(true);
imagen.setVisible(false);
        jLabel2.setVisible(true);
        jLabel3.setVisible(true);
        jLabel4.setVisible(true);
        nom.setVisible(true);
        marc.setVisible(true);
         clav.setVisible(true);
         prec.setVisible(true);
         cantidad.setVisible(true);
         selecciona.setVisible(true);
// TODO add your handling code here:
    }//GEN-LAST:event_editarActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
editar.setVisible(true);
elim.setVisible(true);

       @SuppressWarnings("static-access")
java.sql.Statement st = null;
        try {
             eliminar=jTable1.getValueAt(jTable1.getSelectedRow(),2).toString();
            st = Login.conexion.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(tienda.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            sr = st.executeQuery("SELECT nombres FROM  productos where clave='"+eliminar+"'");
nom.setText(jTable1.getValueAt(jTable1.getSelectedRow(),0).toString());
marc.setText(jTable1.getValueAt(jTable1.getSelectedRow(),1).toString());
clav.setText(jTable1.getValueAt(jTable1.getSelectedRow(),2).toString());
prec.setText(jTable1.getValueAt(jTable1.getSelectedRow(),3).toString());
cantidad.setText(jTable1.getValueAt(jTable1.getSelectedRow(),4).toString());
        } catch (SQLException ex) {
            Logger.getLogger(tienda.class.getName()).log(Level.SEVERE, null, ex);
        }


        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1MouseClicked

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
datosTabla();
editar.setVisible(false);
elim.setVisible(false);
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowOpened

    private void cantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cantidadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cantidadActionPerformed

    private void cambiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cambiarActionPerformed

 
  new contrasen().setVisible(true);
        this.dispose();
// TODO add your handling code here:
    }//GEN-LAST:event_cambiarActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

         Map mapa2 = new HashMap();
       // mapa.put("suma",33);
        //mapa.put("nombre","clarinete");
        Connection c2 = null;
        try {
            c2 = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/desarrollo","root","");
        } catch (SQLException ex) {
            Logger.getLogger(productos.class.getName()).log(Level.SEVERE, null, ex);
        }


        JasperReport reporte2 = null;
            try {
                reporte2 = JasperCompileManager.compileReport("C:/Users/Victor Manuel/Documents/NetBeansProjects/basedeDatos/ticketcompras/reportefinal.jrxml");
            } catch (JRException ex) {
                Logger.getLogger(productos.class.getName()).log(Level.SEVERE, null, ex);
            }
        JasperPrint print2 = null;
            try {
                print2 = JasperFillManager.fillReport(reporte2, mapa2, c2);
            } catch (JRException ex) {
                Logger.getLogger(productos.class.getName()).log(Level.SEVERE, null, ex);
            }

        JasperViewer visor= new JasperViewer(print2);


        visor.setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
new Login().setVisible(true);
          this.dispose();

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed
 public void datosTabla(){
      int cont=0;
    try {
          @SuppressWarnings("static-access")
          java.sql.Statement st = l.conexion.createStatement();

    sr=  st.executeQuery("SELECT nombres,marca,clave,precio,cantidad FROM  productos ");
  while  (sr.next()){

    String nombre=sr.getString("nombres");
    String marca=sr.getString("marca");
    String clave=sr.getString("clave");
    int precio=sr.getInt("precio");
    int canti=sr.getInt("cantidad");

    DefaultTableModel tm= (DefaultTableModel) jTable1.getModel();
    Object nuevo[]={"","","",""};
    tm.addRow(nuevo);
    jTable1.setValueAt(nombre, cont, 0);
    jTable1.setValueAt(marca, cont, 1);
    jTable1.setValueAt(clave, cont, 2);
    jTable1.setValueAt(precio, cont, 3);
     jTable1.setValueAt(canti, cont, 4);
   cont++;
   System.out.println("llenó");


  }
             sr.close();

        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }    // TODO add your handling code here:

  }
    /**
    * @param args the command line arguments
    */


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new tienda().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cambiar;
    private javax.swing.JTextField cantidad;
    private javax.swing.JTextField clav;
    private javax.swing.JButton editar;
    private javax.swing.JButton elim;
    private javax.swing.JSpinner fecha;
    private javax.swing.JButton guardar;
    private javax.swing.JLabel imagen;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField marc;
    private javax.swing.JTextField nom;
    private javax.swing.JTextField prec;
    private javax.swing.JLabel selecciona;
    // End of variables declaration//GEN-END:variables
String eliminar;

    private void executeQuery(String string) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
